package ucn.groupone.parkingad;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		new JSONCall().execute();

		
	}
	private static String readUrl(String urlString) throws Exception {
	    BufferedReader reader = null;
	    try {
	        URL url = new URL(urlString);
	        reader = new BufferedReader(new InputStreamReader(url.openStream()));
	        StringBuffer buffer = new StringBuffer();
	        int read;
	        char[] chars = new char[1024];
	        while ((read = reader.read(chars)) != -1){
	            buffer.append(chars, 0, read); 
	            }
	        return buffer.toString();
    	} finally {
	        if (reader != null)
	            reader.close();
	    }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	 private class JSONCall extends AsyncTask<Void, Void, Void> {

         @Override
         protected Void doInBackground(Void... voids) {
     		try {
    			String str1 = readUrl("http://ucn-parking.herokuapp.com/places.json");
    			Log.d("[onCreate]",str1);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
			return null;
         }      
   }   

}
